<!--
 * @Description:个人资产明细
 * @version: v1.0
 * @Author: Wang Xi
 * @Date: 2023-6-16 18:10:00
 * @LastEditors: WangXi
 * @LastEditTime: 2023-06-17 18:30:01
-->
<template>
	<view class="personal-capital-page">
		<c-layout>
			<c-navigation-bar slot="head" navbarBgColor="transparent" left-icon-border="#fff"
				:title="$t('user.personalAssets')"></c-navigation-bar>
			<view class="content-box">

				<view class="boxs">
					<view class="">
						{{nowMonth}}-{{nowYear}}
						<u-icon name="arrow-down" color="#000" size="32" @click="show = true" style="margin-left: 16rpx;"></u-icon>
					</view>
					<view class="" style="color: rgba(145, 144, 154, 1);font-size: 12px;">
						{{$t('storeManagement.Expenditure')}}${{outAmount.toFixed(2)}}
						{{$t('storeManagement.Income')}}${{inAmount.toFixed(2)}}
					</view>
					<u-picker :confirm-text="$t('system.affirm')" :cancel-text="$t('system.cancel')"
						:default-time="`${nowYear}-${nowMonth}`" v-model="show" mode="time" :params="params" :show-time-tag='false'
						@confirm="confirm"></u-picker>
				</view>

				<scroll-view class="balance-change-list" scroll-y="true">
					<!-- 资金记录列表 -->
					<view class="capital-record-box">
						<scroll-list ref="goodList" :option="queryParams" @refresh="refresh" @load="loadMore">

							<view class="list-content1" v-for="item in currentList.list" :key="item.createTime"
								@click="!tabActive?goDetail(item):''">
								<balanceChangeCard :data="item"></balanceChangeCard>
							</view>
						</scroll-list>


					</view>
				</scroll-view>
			</view>
			<view class="btn-box" slot="footer">
				<u-button @click="path('0')"
					style="border: 2rpx solid #2934D0;color: #2934D0;">{{$t('user.recharge')}}</u-button>
				<u-button @click="path('1')" class="blue">{{$t('user.withdrawal')}}</u-button>
			</view>
		</c-layout>
	</view>
</template>

<script>
	import balanceChangeCard from '../../user/components/balanceChangeCard.vue'
	export default {
		components: {
			balanceChangeCard
		},
		data() {
			return {
				// 当前tab下标 0个人资金 1 平台代币
				tabActive: 0,
				show: false,
				inAmount: 0,
				outAmount: 0,
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false
				},
				nowYear: new Date().getFullYear(),
				nowMonth: (new Date().getMonth() + 1).toString().length == 1 ? '0' + (new Date().getMonth() + 1) : new Date()
					.getMonth() + 1,
				// 个人余额
				personalBalance: {
					isGet: false,
					data: {
						"pageSize": 10,
						"pageNum": 1,

					},
					list: []
				},

			};
		},
		onReady() {
			this.getPersonalBalanceAjax()
			var myDate = new Date();
			this.nowYear = myDate.getFullYear();
			var tMonth = myDate.getMonth();
			var m = tMonth + 1;
			if (m.toString().length == 1) {
				m = "0" + m;
			}
			this.nowMonth = m
			var lastDate = new Date(this.nowYear, this.nowMonth, 0).getDate(); //获取当月最后一日
			this.$u.post('/app/shop/Balance/detail/sum', {
				startTime: Number(this.publicJS.timeZoneSwitching(this.nowYear + '-' + this.nowMonth + '-' + '01', "x",'UTC')),
				endTime: Number(this.publicJS.timeZoneSwitching(this.nowYear + '-' + this.nowMonth + '-' + lastDate+' '+"23:59:59", "x",'UTC'))
			}).then(res => {
				this.inAmount = res.data.inAmount
				this.outAmount = res.data.outAmount
			}).catch(err => {})
		},
		computed: {
			currentList: {
				get() {
					return this.personalBalance
				},
				set() {}
			},
			queryParams() {
				return {
					pageNum: 1,
					size: this.personalBalance.data.pageSize,
					offsetBottom: 10,
					contentHeight: 0,
					safeArea: true,
					auto: false,
					emptyText: this.$t('system.NoRecord'),
					noMoreText: this.$t('system.NoFurtherDataAvailable'),
					showPullDown: false,
					loadDisabled: false
				}
			},
		},
		methods: {

			// 获取选择的时间
			confirm(e) {
				console.log(e)
				this.nowYear = e.year
				this.nowMonth = e.month
				var lastDate = new Date(this.nowYear, this.nowMonth, 0).getDate(); //获取当月最后一日
				console.log(lastDate)
				this.$u.post('/app/shop/Balance/detail/sum', {
					startTime: Number(this.publicJS.timeZoneSwitching(this.nowYear + '-' + this.nowMonth + '-' + '01', "x",'UTC')),
					endTime: Number(this.publicJS.timeZoneSwitching(this.nowYear + '-' + this.nowMonth + '-' + lastDate+' '+"23:59:59", "x",'UTC'))
				}).then(res => {
					this.inAmount = res.data.inAmount
					this.outAmount = res.data.outAmount
				}).catch(err => {})
				this.getPersonalBalanceAjax()
			},

			// 获取店铺余额
			getPersonalBalanceAjax(get = true, more, refresh = 'loadSuccess') {
				var lastDate = new Date(this.nowYear, this.nowMonth, 0).getDate(); //获取当月最后一日
				this.personalBalance.data.startTime = Number(this.publicJS.timeZoneSwitching(this.nowYear + '-' + this.nowMonth +
					'-' + '01', "x",'UTC'))
				this.personalBalance.data.endTime = Number(this.publicJS.timeZoneSwitching(this.nowYear + '-' + this.nowMonth +
					'-' + lastDate+' '+"23:59:59", "x",'UTC'))
				this.$u.post('/app/shop/Balance/detail/list', this.personalBalance.data).then(res => {
					this.personalBalance.isGet = true
					this.personalBalance.list = more ? [...this.personalBalance.list, ...res.data.records ? res.data.records :
							[]
						] : res.data
						.records || []
					this.$refs.goodList[refresh]({
						list: this.personalBalance.list,
						total: res.data.nextPage ? this.personalBalance.list.length + 1 : 0
					});
				}).catch(err => {
					this.$refs.goodList.loadFail()
				})
			},
			loadMore() {
				this.personalBalance.data.pageNum += 1
				this.getPersonalBalanceAjax(false, true)
			},
			refresh(paging) {
				this.personalBalance.list = []
				this.personalBalance.data = { pageNum: paging.page, pageSize: paging.size }
				this.getPersonalBalanceAjax(false, false, 'refreshSuccess')
			},
			// 查看详情
			goDetail(item) {
				uni.navigateTo({
					url: '/pages/user/components/capitalDetail?data=' + JSON.stringify(item)
				})

			},
			path(type) {
				uni.navigateTo({
					url: '/pages/storyType/storeManagement/withdrawal?type=' + type + '&rechargeType=1',
					"animationType": "slide-in-right",
					"animationDuration": 300
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	/deep/ .left-icon-block {
		background: #fff;
	}

	.personal-capital-page {
		background: rgba(247, 248, 250, 1);

		.content-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 100%;
			padding-top: 16rpx;
			height: 100%;

			.tabs-box {
				width: 452rpx;
				background: #fff;
				height: 84rpx;
				box-shadow: 0rpx 8rpx 20rpx 0rpx rgba(0, 0, 0, 0.2);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				display: flex;
				overflow: hidden;

				>.don {
					width: 28rpx;
					// transition: .2s;

					&.is-rotate {
						transform: rotate(180deg);
					}
				}

				>.tab {
					// transition: .3s;
					width: 212rpx;
					color: #91909A;
					text-align: center;
					line-height: 84rpx;

					&.is-active {
						background-color: #2934D0;
						color: #fff;
					}
				}
			}

			.boxs {
				height: 110rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				padding: 0 40rpx;
			}

			.balance-change-list {
				background: #fff;
				flex: 1;
				height: 0;
				padding-top: 32rpx;

				.capital-record-box {
					padding: 0 40rpx;
					height: 100%;

					>view {
						margin-bottom: 32rpx;
						height: 100%;
					}
				}
			}
		}

		.list-content1 {
			margin: 40rpx 0;
		}

		.btn-box {
			display: flex;
			justify-content: space-between;
			padding: 30rpx 40rpx;

			>.u-btn {
				margin: 0;
				width: 316rpx;
				height: 120rpx;
			}
		}
	}

	/deep/ .c-layout-container .layout-footer {
		position: relative;
		bottom: 0;
		background: #fff;
		z-index: 1;
		border-radius: 36rpx 36rpx 0 0;
		box-shadow: 0px -1px 10px rgba(0, 0, 0, 0.1);
	}

	/deep/ .scroll-list-wrap .scroll-view {
		height: 100% !important;
	}
</style>